<script setup>
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { loginApi } from '@/api/user';
import { useRouter } from 'vue-router';

// 路由对象
const router = useRouter();

// 表单内容
const logininfo = ref({
  username: '',
  password: '',
});

// 是否记住账户
const remember = ref(false);
// 登录返回信息
const userinfo = ref();
const loading = ref(false);
const showPassword = ref(false);

// 表单校验规则
const rules = ref({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
});

//登录请求
const loginSubmit = async () => {
  // 发送登录请求
  const result = await loginApi(logininfo.value);
  if (result.code) {
    //提示登录信息
    ElMessage.success('登录成功');
    //存储用户数据和token
    localStorage.setItem('loginUser', JSON.stringify(result.data));
    //跳转到首页
    router.push('/');
  }else{
    ElMessage.error(result.message || '登录失败，请重试');
  }
};

</script>


<template>
  <div class="login-container">
    <div class="login-card">

      <div class="login-header">
        <h1>环境监测系统登录</h1>
      </div>
      
      <el-form 
        ref="formRef"
        :model="logininfo" 
        :rules="rules" 
        class="login-form"
        label-position="left"
        label-width="80px"
        @submit.native.prevent="loginSubmit"
      >
        <el-form-item prop="username" label="用户名">
          <el-input 
            v-model="logininfo.username" 
            placeholder="请输入用户名"
            prefix-icon="User"
            size="large"
          />
        </el-form-item>
        
        <el-form-item prop="password" label="密码">
          <el-input 
            v-model="logininfo.password"
            :type="showPassword ? 'text' : 'password'" 
            placeholder="请输入密码"
            prefix-icon="Lock"
            size="large"
          >
            <template #suffix>
              <el-icon 
                class="password-toggle-icon" 
                @click="showPassword = !showPassword"
              >
                <component :is="showPassword ? 'View' : 'Hide'" />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <div class="form-options">
          <el-checkbox v-model="logininfo.remember">记住我</el-checkbox>
          <el-link type="primary" href="#" class="forgot-password">忘记密码?</el-link>
        </div>
        
        <el-button 
          type="primary" 
          :loading="loading"
          class="login-button"
          @click="loginSubmit"
          size="large"
          round
        >
          {{ loading ? '登录中...' : '登录' }}
        </el-button>
      </el-form>
      
      <div class="login-footer">
        <p>还没有账号? <el-link type="primary" href="#">立即注册</el-link></p>
      </div>
    </div>
  </div>
</template>



<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.05);
  padding: 40px;
  transition: all 0.3s ease;
}

.login-header {
  text-align: center;
  margin-bottom: 36px;
}

.login-header h1 {
  margin: 0 0 8px;
  font-size: 28px;
  font-weight: 600;
  color: #333;
}

.login-header p {
  color: #666;
  font-size: 16px;
  margin: 0;
}

.login-form {
  margin-bottom: 24px;
}

/* 添加表单对齐样式 */
:deep(.el-form-item__label) {
  width: 70px;
  text-align: left;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.password-toggle-icon {
  cursor: pointer;
  color: #909399;
  font-size: 16px;
  transition: color 0.2s;
}

.password-toggle-icon:hover {
  color: #409EFF;
}

.forgot-password {
  color: #4a6cf7;
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: color 0.3s;
}

.forgot-password:hover {
  color: #2d4ed8;
  text-decoration: underline;
}

.login-button {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 8px;
  background-color: #4a6cf7;
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
}

.login-button:hover {
  background-color: #3a5be0;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(74, 108, 247, 0.2);
}

.login-button:disabled {
  background-color: #a0aef0;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.login-footer {
  text-align: center;
  color: #666;
  font-size: 14px;
}

.login-footer a {
  color: #4a6cf7;
  font-weight: 500;
  text-decoration: none;
  transition: color 0.3s;
}

.login-footer a:hover {
  color: #2d4ed8;
  text-decoration: underline;
}

@media (max-width: 480px) {
  .login-card {
    padding: 30px 20px;
  }
  
  .login-header h1 {
    font-size: 24px;
  }
}
</style>